package com.rfid.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import biz.util.*;

import com.rfid.project.model.*;
import com.rfid.project.dao.*;
import com.rfid.project.exception.CannotDelException;
import com.mysema.query.BooleanBuilder;

import java.util.List;

@Service
@Transactional
public class ProjectSourceService{

	@Autowired
	private ProjectSourceDao projectSourceDao;
	
	private QProjectSource $ = QProjectSource.projectSource;

	@Autowired
	private ProjectDao projectDao;
	
	private QProject $_ = QProject.project;
	
	@Autowired
	public void setProjectSourceDao(ProjectSourceDao projectSourceDao) {
		this.projectSourceDao = projectSourceDao;
	}
	
	@Autowired
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	
	public List<ProjectSource> findAll(){
		return ListUtil.toList(projectSourceDao.findAll());
	}
				
	public List<ProjectSource> findAll(
	String name,
	String remark,
	String orderByAttribute	){
	BooleanBuilder condition = new BooleanBuilder();
	
	if(StringUtil.isNotEmpty(name)){
		condition.and($.name.contains(name));
	}
	
	
	if(StringUtil.isNotEmpty(remark)){
		condition.and($.remark.contains(remark));
	}
	
	
	return ListUtil.toList(projectSourceDao.findAll(condition));
	}		

	public Page<ProjectSource> findAll(
			String name,
			String remark,
			int pageNumber, int pageSize,String sortAttribute,String orderDirection) {
			BooleanBuilder condition = new BooleanBuilder();
	
			if(StringUtil.isNotEmpty(name)){
				condition.and($.name.contains(name));
			}
			if(StringUtil.isNotEmpty(remark)){
				condition.and($.remark.contains(remark));
			}
	
			/*
			Sort sortObject = null;
			if(StringUtil.isEmpty(sortAttribute)){
				sortObject = new Sort(Direction.DESC, "id");
			}else{
				sortObject = new Sort(Direction.fromStringOrNull(orderDirection),sortAttribute);
			}
			*/
			
			Sort sortObject_1 = new Sort(Direction.DESC, "orderNumber");
			
			PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sortObject_1);
			return projectSourceDao.findAll(condition, pageRequest);
	}
	
	public ProjectSource getProjectSource(Long id){
		return projectSourceDao.getOne(id);
	}
	
	public void deleteProjectSource(Long id) throws CannotDelException{
		if (this.findProjectByProjectSource(id)) {
			throw new CannotDelException("项目来源已经被项目引用，请先删除项目或修改项目的引用！");
		}else{
			projectSourceDao.delete(id);
		}
	}

	public void saveProjectSource(ProjectSource projectSource) {
		if (null == projectSource.getOrderNumber()) {
			projectSource.setOrderNumber(this.getOrderNumber());
		}
		projectSourceDao.save(projectSource);
	}

	private int getOrderNumber(){
		BooleanBuilder condition = new BooleanBuilder();
		Sort sortObject = new Sort(Direction.DESC, "orderNumber");
		PageRequest pageRequest = new PageRequest(0, 1, sortObject);
		Page<ProjectSource> projectSources = this.projectSourceDao.findAll(condition,pageRequest);
		if(projectSources.getTotalElements() > 0){
			ProjectSource authority = projectSources.getContent().get(0);
			return authority.getOrderNumber()+1;
		}
		return 1;
	}
	
	/**
	 * 通过项目来源ID查询是否有项目引用了项目来源
	 * @param projectSourceId 项目来源ID
	 * @return 是否有项目引用了项目来源
	 */
	private boolean findProjectByProjectSource(Long projectSourceId){
		BooleanBuilder condition = new BooleanBuilder();
		condition.and($_.projectSource.id.eq(projectSourceId));
		return this.projectDao.count(condition) > 0;
	}
	
	public boolean changeOrderNumber(Long id, Long targetId){
		ProjectSource currentModule = this.projectSourceDao.findOne(id);
		ProjectSource targetModule = this.projectSourceDao.findOne(targetId);
		int orderNumber = currentModule.getOrderNumber();
		currentModule.setOrderNumber(targetModule.getOrderNumber());
		targetModule.setOrderNumber(orderNumber);
		
		this.projectSourceDao.save(currentModule);
		this.projectSourceDao.save(targetModule);
		return true;
	}

	public ProjectSource findAll(Long id) {
		return this.projectSourceDao.findOne(id);
	}
	

}
		
